package top.kaoshanji.r1ch06b;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 代码清单-r1ch06b-215页a：实现ApplicationContextAware接口拥有发布事件的能力
 * @author kaoshanji
 * ^_^
 * create time 2025/5/6 19:55
 */
public class MailSender implements ApplicationContextAware {

    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public void sendMail(String to) {
        System.out.println("MailSender:模拟发送邮件...");
        MailSendEvent mse = new MailSendEvent(this.context, to);

        context.publishEvent(mse);
    }

}
